home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / Tickle-4.0 (tcl) / tcl / extend / tclsrc / buildhelp.tcl < prev    next >
Encoding:
Text File  |  1993-10-26  |  12.3 KB  |  377 lines  |  [TEXT/MPS ]

  1. #
  2. # buildhelp.tcl --
  3. #
  4. # Program to extract help files from TCL manual pages or TCL script files.
  5. # The help directories are built as a hierarchical tree of subjects and help
  6. # files.  
  7. #------------------------------------------------------------------------------
  8. # Copyright 1992-1993 Karl Lehenbauer and Mark Diekhans.
  9. #
  10. # Permission to use, copy, modify, and distribute this software and its
  11. # documentation for any purpose and without fee is hereby granted, provided
  12. # that the above copyright notice appear in all copies.  Karl Lehenbauer and
  13. # Mark Diekhans make no representations about the suitability of this
  14. # software for any purpose.  It is provided "as is" without express or
  15. # implied warranty.
  16. #------------------------------------------------------------------------------
  17. # $Id: buildhelp.tcl,v 2.10 1993/08/31 23:44:51 markd Exp $
  18. #------------------------------------------------------------------------------
  19. #
  20. # For nroff man pages, the areas of text to extract are delimited with:
  21. #
  22. #     '@help: subjectdir/helpfile
  23. #     '@endhelp
  24. #
  25. # start in column one. The text between these markers is extracted and stored
  26. # in help/subjectdir/help.  The file must not exists, this is done to enforced 
  27. # cleaning out the directories before help file generation is started, thus
  28. # removing any stale files.  The extracted text is run through:
  29. #
  30. #     nroff -man|col -xb   {col -b on BSD derived systems}
  31. #
  32. # If there is other text to include in the helpfile, but not in the manual 
  33. # page, the text, along with nroff formatting commands, may be included using:
  34. #
  35. #     '@:Other text to include in the help page.
  36. #
  37. # A entry in the brief file, used by apropos my be included by:
  38. #
  39. #     '@brief: Short, one line description
  40. #
  41. # These brief request must occur with in the bounds of a help section.
  42. #
  43. # If some header text, such as nroff macros, need to be preappended to the
  44. # text streem before it is run through nroff, then that text can be bracketed
  45. # with:
  46. #
  47. #     '@header
  48. #     '@endheader
  49. #
  50. # If multiple header blocks are encountered, they will all be preappended.
  51. #
  52. # For TCL script files, which are indentified because they end in ".tcl",
  53. # the text to be extracted is delimited by:
  54. #
  55. #    #@help: subjectdir/helpfile
  56. #    #@endhelp
  57. #
  58. # And brief lines are in the form:
  59. #
  60. #     #@brief: Short, one line description
  61. #
  62. # The only processing done on text extracted from .tcl files it to replace
  63. # the # in column one with a space.
  64. #
  65. #
  66. #-----------------------------------------------------------------------------
  67. # To generate help:
  68. #
  69. #   buildhelp helpDir brief.brf filelist
  70. #
  71. # o helpDir is the help tree root directory.  helpDir should  exists, but any
  72. #   subdirectories that don't exists will be created.  helpDir should be
  73. #   cleaned up before the start of manual page generation, as this program
  74. #   will not overwrite existing files.
  75. # o brief.brf  is the name of the brief file to create form the @brief entries.
  76. #   It must have an extension of ".brf".  It will be created in helpDir.
  77. # o filelist are the nroff manual pages, or .tcl, .tlib files to extract
  78. #   the help files from. If the suffix is not .tcl or .tlib, a nroff manual
  79. #   page is assumed.
  80. #
  81. #-----------------------------------------------------------------------------
  82.  
  83. #@package: TclX-buildhelp buildhelp
  84.  
  85. #-----------------------------------------------------------------------------
  86. # Truncate a file name of a help file if the system does not support long
  87. # file names.  If the name starts with `Tcl_', then this prefix is removed.
  88. # If the name is then over 14 characters, it is truncated to 14 charactes
  89. #  
  90. proc TruncFileName {pathName} {
  91.     global G_truncFileNames
  92.  
  93.     if {!$G_truncFileNames} {
  94.         return $pathName}
  95.     set fileName [file tail $pathName]
  96.     if {"[crange $fileName 0 3]" == "Tcl_"} {
  97.         set fileName [crange $fileName 4 end]}
  98.     set fileName [crange $fileName 0 13]
  99.     return "[file dirname $pathName]/$fileName"
  100. }
  101.  
  102. #-----------------------------------------------------------------------------
  103. # Proc to ensure that all directories for the specified file path exists,
  104. # and if they don't create them.  Don't use -path so we can set the
  105. # permissions.
  106.  
  107. proc EnsureDirs {filePath} {
  108.     set dirPath [file dirname $filePath]
  109.     if [file exists $dirPath] return
  110.     foreach dir [split $dirPath /] {
  111.         lappend dirList $dir
  112.         set partPath [join $dirList /]
  113.         if [file exists $partPath] continue
  114.  
  115.         mkdir $partPath
  116.         chmod u=rwx,go=rx $partPath
  117.     }
  118. }
  119.  
  120.  
  121. #-----------------------------------------------------------------------------
  122. #
  123. # Proc to extract nroff text to use as a header to all pass to nroff when
  124. # processing a help file.
  125. #    manPageFH - The file handle of the manual page.
  126. #
  127.  
  128. proc ExtractNroffHeader {manPageFH} {
  129.     global nroffHeader
  130.     while {[gets $manPageFH manLine] >= 0} {
  131.         if {[string first "'@endheader" $manLine] == 0} {
  132.             break;
  133.             }
  134.         if {[string first "'@:" $manLine] == 0} {
  135.             set manLine [csubstr manLine 3 end]
  136.             }
  137.         append nroffHeader "$manLine\n"
  138.         }
  139. }
  140.  
  141. #-----------------------------------------------------------------------------
  142. #
  143. # Proc to extract a nroff help file when it is located in the text.
  144. #    manPageFH - The file handle of the manual page.
  145. #    manLine - The '@help: line starting the data to extract.
  146. #
  147.  
  148. proc ExtractNroffHelp {manPageFH manLine} {
  149.     global G_helpDir nroffHeader G_briefHelpFH G_colArgs
  150.  
  151.     set helpName [string trim [csubstr $manLine 7 end]]
  152.     set helpFile [TruncFileName "$G_helpDir/$helpName"]
  153.     if {[file exists $helpFile]} {
  154.         error "Help file already exists: $helpFile"}
  155.     EnsureDirs $helpFile
  156.     set helpFH [open "| nroff -man | col $G_colArgs > $helpFile" w]
  157.     echo "    creating help file $helpName"
  158.  
  159.     # Nroff commands from .TH macro to get the formatting right.  
  160.     # The `\\n' become `\n' in the text.
  161.         
  162.     puts $helpFH ".ad b"
  163.     puts $helpFH ".PD"
  164.     puts $helpFH ".nrIN \\n()Mu"
  165.     puts $helpFH ".nr)R 0"
  166.     puts $helpFH ".nr)I \\n()Mu"
  167.     puts $helpFH ".nr)R 0"
  168.     puts $helpFH ".\}E"
  169.     puts $helpFH ".DT"
  170.     puts $helpFH ".hy14"
  171.     puts $helpFH $nroffHeader
  172.     set foundBrief 0
  173.     while {[gets $manPageFH manLine] >= 0} {
  174.         if {[string first "'@endhelp" $manLine] == 0} {
  175.             break;
  176.         }
  177.         if {[string first "'@brief:" $manLine] == 0} {
  178.             if $foundBrief {
  179.                 error {Duplicate "'@brief" entry"}
  180.             }
  181.             set foundBrief 1
  182.         puts $G_briefHelpFH "$helpName\t[csubstr $manLine 8 end]"
  183.             continue;
  184.         }
  185.         if {[string first "'@:" $manLine] == 0} {
  186.             set manLine [csubstr $manLine 3 end]
  187.         }
  188.         if {[string first "'@help" $manLine] == 0} {
  189.             error {"'@help" found within another help section"}
  190.         }
  191.         puts $helpFH $manLine
  192.         }
  193.     close $helpFH
  194.     chmod a-w,a+r $helpFile
  195. }
  196.  
  197. #-----------------------------------------------------------------------------
  198. #
  199. # Proc to extract a tcl script help file when it is located in the text.
  200. #    ScriptPageFH - The file handle of the .tcl file.
  201. #    ScriptLine - The #@help: line starting the data to extract.
  202. #
  203.  
  204. proc ExtractScriptHelp {ScriptPageFH ScriptLine} {
  205.     global G_helpDir G_briefHelpFH
  206.     set helpName [string trim [csubstr $ScriptLine 7 end]]
  207.     set helpFile "$G_helpDir/$helpName"
  208.     if {[file exists $helpFile]} {
  209.         error "Help file already exists: $helpFile"}
  210.     EnsureDirs $helpFile
  211.     set helpFH [open $helpFile w]
  212.     echo "    creating help file $helpName"
  213.     set foundBrief 0
  214.     while {[gets $ScriptPageFH ScriptLine] >= 0} {
  215.         if {[string first "#@endhelp" $ScriptLine] == 0} {
  216.             break;
  217.         }
  218.         if {[string first "#@brief:" $ScriptLine] == 0} {
  219.             if $foundBrief {
  220.                 error {Duplicate "#@brief" entry"}
  221.             }
  222.             set foundBrief 1
  223.         puts $G_briefHelpFH "$helpName\t[csubstr $ScriptLine 8 end]"
  224.             continue;
  225.         }
  226.         if {[string first "#@help" $ScriptLine] == 0} {
  227.             error {"#@help" found within another help section"}
  228.         }
  229.         if {[clength $ScriptLine] > 1} {
  230.             set ScriptLine " [csubstr $ScriptLine 1 end]"
  231.         } else {
  232.             set ScriptLine ""
  233.         }
  234.         puts $helpFH $ScriptLine
  235.         }
  236.     close $helpFH
  237.     chmod a-w,a+r $helpFile
  238. }
  239.  
  240. #-----------------------------------------------------------------------------
  241. #
  242. # Proc to scan a nroff manual file looking for the start of a help text
  243. # sections and extracting those sections.
  244. #    pathName - Full path name of file to extract documentation from.
  245. #
  246.  
  247. proc ProcessNroffFile {pathName} {
  248.    global G_nroffScanCT G_scriptScanCT nroffHeader
  249.  
  250.    set fileName [file tail $pathName]
  251.  
  252.    set nroffHeader {}
  253.    set manPageFH [open $pathName r]
  254.    echo "    scanning $pathName"
  255.    set matchInfo(fileName) [file tail $pathName]
  256.    scanfile $G_nroffScanCT $manPageFH
  257.    close $manPageFH
  258. }
  259.  
  260. #-----------------------------------------------------------------------------
  261. #
  262. # Proc to scan a Tcl script file looking for the start of a
  263. # help text sections and extracting those sections.
  264. #    pathName - Full path name of file to extract documentation from.
  265. #
  266.  
  267. proc ProcessTclScript {pathName} {
  268.    global G_scriptScanCT nroffHeader
  269.  
  270.    set scriptFH [open "$pathName" r]
  271.  
  272.    echo "    scanning $pathName"
  273.    set matchInfo(fileName) [file tail $pathName]
  274.    scanfile $G_scriptScanCT $scriptFH
  275.    close $scriptFH
  276. }
  277.  
  278. #-----------------------------------------------------------------------------
  279. # build: main procedure.  Generates help from specified files.
  280. #    helpDirPath - Directory were the help files go.
  281. #    briefFile - The name of the brief file to create.
  282. #    sourceFiles - List of files to extract help files from.
  283.  
  284. proc buildhelp {helpDirPath briefFile sourceFiles} {
  285.     global G_helpDir G_truncFileNames G_nroffScanCT
  286.     global G_scriptScanCT G_briefHelpFH G_colArgs
  287.  
  288.     echo ""
  289.     echo "Begin building help tree"
  290.  
  291.     # Determine version of col command to use (no -x on BSD)
  292.     if {[system {col -bx </dev/null >/dev/null 2>&1}] != 0} {
  293.         set G_colArgs {-b}
  294.     } else {
  295.         set G_colArgs {-bx}
  296.     }
  297.     set G_helpDir $helpDirPath
  298.     if {![file exists $G_helpDir]} {
  299.         mkdir $G_helpDir
  300.     }
  301.  
  302.     if {![file isdirectory $G_helpDir]} {
  303.         error [concat "$G_helpDir is not a directory or does not exist. "  
  304.                       "This should be the help root directory"]
  305.     }
  306.         
  307.     set status [catch {set tmpFH [open $G_helpDir/AVeryVeryBigFileName w]}]
  308.     if {$status != 0} {
  309.         set G_truncFileNames 1
  310.     } else {
  311.         close $tmpFH
  312.         unlink $G_helpDir/AVeryVeryBigFileName
  313.         set G_truncFileNames 0
  314.     }
  315.  
  316.     set G_nroffScanCT [scancontext create]
  317.  
  318.     scanmatch $G_nroffScanCT "^'@help:" {
  319.         ExtractNroffHelp $matchInfo(handle) $matchInfo(line)
  320.         continue
  321.     }
  322.  
  323.     scanmatch $G_nroffScanCT "^'@header" {
  324.         ExtractNroffHeader $matchInfo(handle)
  325.         continue
  326.     }
  327.     scanmatch $G_nroffScanCT "^'@endhelp" {
  328.         error [concat {"'@endhelp" without corresponding "'@help:"} \
  329.                  ", offset = $matchInfo(offset)"]
  330.     }
  331.     scanmatch $G_nroffScanCT "^'@brief" {
  332.         error [concat {"'@brief" without corresponding "'@help:"} \
  333.                  ", offset = $matchInfo(offset)"]
  334.     }
  335.  
  336.     set G_scriptScanCT [scancontext create]
  337.     scanmatch $G_scriptScanCT "^#@help:" {
  338.         ExtractScriptHelp $matchInfo(handle) $matchInfo(line)
  339.     }
  340.  
  341.     if {[file extension $briefFile] != ".brf"} {
  342.         puts stderr "Brief file \"$briefFile\" must have an extension \".brf\""
  343.         exit 1
  344.     }
  345.     if [file exists $G_helpDir/$briefFile] {
  346.         puts stderr "Brief file \"$G_helpDir/$briefFile\" already exists"
  347.         exit 1
  348.     }
  349.     set G_briefHelpFH [open "|sort > $G_helpDir/$briefFile" w]
  350.  
  351.     foreach manFile $sourceFiles {
  352.         set manFile [glob $manFile]
  353.         set ext [file extension $manFile]
  354.         if {$ext == ".tcl" || $ext == ".tlib"} {
  355.             set status [catch {ProcessTclScript $manFile} msg]
  356.         } else {
  357.             set status [catch {ProcessNroffFile $manFile} msg]
  358.         }
  359.         if {$status != 0} {
  360.             echo "Error extracting help from: $manFile"
  361.             echo $msg
  362.             global errorInfo tcl_interactive
  363.             if {!$tcl_interactive} {
  364.                 echo $errorInfo
  365.                 exit 1
  366.             }
  367.         }
  368.     }
  369.  
  370.     close $G_briefHelpFH
  371.     chmod a-w,a+r $G_helpDir/$briefFile
  372.     echo "Completed extraction of help files"
  373. }
  374.  
  375.